package com.elitesland.system.config;

import com.elitesland.security.config.bean.JwtProperties;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

//import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;

/**
 * Swagger2全局配置
 *
 * @author Mir
 * @date 2018-11-08
 */
@Configuration
@RequiredArgsConstructor
public class SystemSwagger2Config {
    /**
     * 标题
     */
    @Value("${swagger.title}")
    private String title;
    /**
     * 基本包
     */
    @Value("${swagger.base.package}")
    private String basePackage;
    /**
     * 描述
     */
    @Value("${swagger.description}")
    private String description;
    /**
     * URL
     */
    @Value("${swagger.url}")
    private String url;
    /**
     * 作者
     */
    @Value("${swagger.contact.name}")
    private String contactName;
    /**
     * 作者网址
     */
    @Value("${swagger.contact.url}")
    private String contactUrl;
    /**
     * 作者邮箱
     */
    @Value("${swagger.contact.email}")
    private String contactEmail;
    /**
     * 版本
     */
    @Value("${swagger.version}")
    private String version;

    private final JwtProperties jwtProperties;

    @Bean
    public Docket createSystemRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("系统模块API")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.elitesland.system"))
                .paths(PathSelectors.any())
                .build()
//                .globalRequestParameters(setHeaderToken())
                ;
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(title)
                .description(description)
                .termsOfServiceUrl(url)
                .contact(new Contact(contactName, contactUrl, contactEmail))
                .version(version)
                .build();
    }

//    private List<RequestParameter> setHeaderToken() {
//        List<RequestParameter> pars = new ArrayList<>();
//        // token请求头
//        RequestParameterBuilder tokenPar = new RequestParameterBuilder();
//        RequestParameter tokenParameter = tokenPar
//                .name(jwtProperties.getHeader())
//                .description("JWT Token Request Header")
//                .required(false)
//                .in(ParameterType.HEADER)
//                .query(q -> q.model(m -> m.scalarModel(ScalarType.STRING)))
//                .build();
//        pars.add(tokenParameter);
//        return pars;
//    }
}
